package com.bw.producer.service.impl;

import com.bw.producer.Vo.AttrGroupVo;
import com.bw.producer.Vo.ItemInfoVo;
import com.bw.producer.Vo.SaleAttrVo;
import com.bw.producer.config.ItemThreadPoolProperties;
import com.bw.producer.config.ProductThreadPoolConfig;
import com.bw.producer.domain.PmsSkuImages;
import com.bw.producer.domain.PmsSkuInfo;
import com.bw.producer.domain.PmsSpuInfoDesc;
import com.bw.producer.mapper.ItemDetailsMapper;
import com.bw.producer.service.IPmsSkuInfoService;
import com.bw.producer.service.ItemDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
public class ItemDetailsServiceImpl implements ItemDetailsService {

    @Autowired
    private ItemDetailsMapper itemDetailsMapper;

    @Autowired
    private IPmsSkuInfoService iPmsSkuInfoService;

    @Autowired
    private ItemThreadPoolProperties itemThreadPoolProperties;

    @Autowired
    private ProductThreadPoolConfig productThreadPoolConfig;

    /**
     * 详情项
     * @param skuId
     * @return
     */
    @Override
    public ItemInfoVo itemDetails(Long skuId) {
        ItemInfoVo itemInfoVo = new ItemInfoVo();
        CompletableFuture<PmsSkuInfo> pmsSkuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            PmsSkuInfo pmsSkuInfos = itemDetailsMapper.selectPmsSkuInfo(skuId);
            itemInfoVo.setSkuInfo(pmsSkuInfos);
            return pmsSkuInfos;
        }, productThreadPoolConfig.productThreadPool(itemThreadPoolProperties));

        CompletableFuture<Void> pmsSkuImagesTableFuture = CompletableFuture.runAsync(() -> {
            List<PmsSkuImages> pmsSkuImages = itemDetailsMapper.selectSkuImages(skuId);
            itemInfoVo.setSkuImagesList(pmsSkuImages);
        }, productThreadPoolConfig.productThreadPool(itemThreadPoolProperties));

        CompletableFuture<Void> saleAttrVosTableFuture = CompletableFuture.runAsync(() -> {
            List<SaleAttrVo> saleAttrVos = iPmsSkuInfoService.selectPmsSkuInfoListBySkuId(skuId);
            itemInfoVo.setSaleAttrs(saleAttrVos);
        }, productThreadPoolConfig.productThreadPool(itemThreadPoolProperties));

        CompletableFuture<Void> SpuInfoDescTableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((pmsSkuInfo) -> {
            PmsSpuInfoDesc pmsSpuInfoDesc = itemDetailsMapper.selectSpuInfoDesc(pmsSkuInfo.getSpuId());
            itemInfoVo.setSpuInfoDesc(pmsSpuInfoDesc);
        }, productThreadPoolConfig.productThreadPool(itemThreadPoolProperties));

        CompletableFuture<Void> AttrGroupListBySkuIdTableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync((pmsSkuInfo) -> {
            List<AttrGroupVo> attrGroupVos = itemDetailsMapper.selectAttrGroupListBySkuId(pmsSkuInfo.getCatalogId());
            itemInfoVo.setAttrGroupVos(attrGroupVos);
        }, productThreadPoolConfig.productThreadPool(itemThreadPoolProperties));

        CompletableFuture<Void> allOf = CompletableFuture.allOf(
                pmsSkuInfoCompletableFuture,
                pmsSkuImagesTableFuture,
                saleAttrVosTableFuture,
                SpuInfoDescTableFuture,
                AttrGroupListBySkuIdTableFuture
        );

        allOf.join();
        return itemInfoVo;
    }
}
